let express = require('express');
let path = require('path');
let favicon = require('serve-favicon');
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
// 引用express-session模块
let session = require('express-session');

/*这里写 routes js 地址*/
let index = require('./routes/index');
let login = require('./routes/login');
let register = require('./routes/register');
let publish = require('./routes/user/index');
let settings = require('./routes/user/settings');
let publish_post = require('./routes/api/index');
let admin = {
	index: require('./routes/admin'),
	login: require('./routes/admin/login'),
	user: {
		index: require('./routes/admin/user/index')
	},
	article: {
		index: require('./routes/admin/article/index')
	},
	SiteConfig: {
		index: require('./routes/admin/SiteConfig/index'),
		seo: require('./routes/admin/SiteConfig/seo')
	},
	link:{
		index:require('./routes/admin/link/index')
	},
	api: {
		index: require('./routes/admin/api/index')
	}
}

let app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
	extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

/**
 *  使用session模块
 *  这里不使用数据库存储session， 不使用数据库存储session， 不使用数据库存储session
 *  使用内存的session存储
 **/
app.use(session({
	secret: 'demo_test',
	name: 'mydemo', //这里的name值得是cookie的name，默认cookie的name是：connect.sid
	cookie: {
		maxAge: 30 * 60 * 1000
	}, //设置maxAge是30分钟，即30分钟后session和相应的cookie失效过期
	resave: false, // 每次请求都重新设置session cookie
	saveUninitialized: true // 无论有没有session cookie，每次请求都设置个session cookie
}));

/**
 *  中间件
 *  接口请求之后，更新session的时间
 *  use this middleware to reset cookie expiration time
 *  when user hit page every time
 **/
app.use(function(req, res, next) {
	req.session._garbage = Date();
	req.session.touch();
	next();
});

/* 配置路由 */
app.use('/', index);
app.use('/login', login);
app.use('/register', register);
app.use('/user', publish);
app.use('/api/index/', publish_post);
app.use('/user/settings',settings);
app.use('/admin', admin.index);
app.use('/admin/user/', admin.user.index);
app.use('/admin/article/', admin.article.index);
app.use('/admin/SiteConfig/',admin.SiteConfig.index);
app.use('/admin/SiteConfig/seo',admin.SiteConfig.seo);
app.use('/admin/link/',admin.link.index);
app.use('/admin/api/index', admin.api.index);
app.use('/admin/login/', admin.login);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
	let err = new Error('Not Found');
	err.status = 404;
	next(err);
});

// error handler
app.use(function(err, req, res, next) {
	// set locals, only providing error in development
	res.locals.message = err.message;
	res.locals.error = req.app.get('env') === 'development' ? err : {};

	// render the error page
	res.status(err.status || 500);
	res.render('error');
});

module.exports = app;